package com.google.code.UtopiaXml;

/**
 * Class representing xs:element with maxOccurs&lt;=1.
 *
 * @author Michael Simacek
 */
public class SingleElement extends Element {

    /**
     * Creates element with given type and name
     *
     * @param type type of the element
     * @param name original (unconverted) name of the element
     */
    public SingleElement(Type type, String name) {
        super(name, new SingleProperty(type, name));
    }

    @Override
    public void generateParserBlock(CodeBuilder cb, String parentObject, String nodeName) {
        cb.appendLine("//begin SingleElement");
        String resName = getName() + "Object";
        getType().appendLoaderFromElement(cb, nodeName, resName);
        cb.appendLine(parentObject + ".set" + CodeBuilder.toUpperCamelCase(getName()) + "(" + resName + ");");
        cb.appendLine(nodeName + " = getNextElement(" + nodeName + ");");
        cb.appendLine("//end SingleElement");
    }
}
